package com.nimitz.web.controller.project;


import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.project.domain.ProjectFactory;
import com.nimitz.project.service.ProjectFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/smart/project")
public class ProjectController extends BaseController {

    @Autowired
    private ProjectFactoryService projectFactoryService;

    @PostMapping("/add")
    public AjaxResult addProject(@RequestBody ProjectFactory project) {
        projectFactoryService.addProject(project);
        return AjaxResult.success();
    }
    @GetMapping("/selectById")
    public AjaxResult selectById(@RequestParam Long id){
        List<ProjectFactory> project = projectFactoryService.selectById(id);
        return AjaxResult.success(project);
    }
    @PutMapping("/updateState")
    public AjaxResult updateTaskState(@RequestBody ProjectFactory project) {
        projectFactoryService.updateProjectState(project);
        return AjaxResult.success();
    }
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "areaId",required = false) Long areaId) {
        System.out.println("Received areaId: " + areaId);
        startPage();
        List<ProjectFactory> list;
        if (areaId != null) {
            // 当 areaId 存在时，返回特定区域的项目
            list = projectFactoryService.getProjectsByArea(areaId);
        } else {
            // 当 areaId 不存在时，返回所有项目
            list = projectFactoryService.getAllProjects(); // 假设这个方法返回所有项目
        }
        return getDataTable(list);
    }
}
